/*
* Copyright (c) 2024 SwanLink (Jiangsu) Technology Development Co., LTD.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*     http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import 'package:flutter/src/widgets/framework.dart';
import 'package:flutter_yfree/YFree.dart';
import 'package:flutter/material.dart';

class MyUtils extends YPage {
  var title = "";

  @override
  void yOnCreate() {
    setState(() => title = yGetAppName());
    yBroadcast_register("MyBroadcast", (params) => yToast(params!["msg"]));
  }

  @override
  void initState() {
    super.initState();
    yBroadcast_send("MyBroadcast", params: {"msg": "initState"});
  }

  @override
  void yOnDestory() {
    super.yOnDestory();
    yBroadcast_send("MyBroadcast", params: {"msg": "yOnDestory"});
  }

  @override
  Future yOnYFreeCall(String method, Map arguments) {
    return super.yOnYFreeCall(method, arguments);
  }

  @override
  void yOnReceiveBroadcast(String broadCastName, Map arguments) {
    super.yOnReceiveBroadcast(broadCastName, arguments);
  }

  @override
  bool ySafeAreaBottom() {
    return false;
  }

  Widget view_button(String title, Function() onTap) {
    return YButton(
      child: Text(title,
          style: const TextStyle(
              color: Colors.white, fontSize: 20, fontWeight: FontWeight.bold)),
      padding: const EdgeInsets.fromLTRB(10, 5, 10, 5),
      color: YConfig.themeColor,
      onTap: onTap,
    );
  }

  @override
  Widget yBuild(BuildContext context) {
    return Container(
      color: Colors.white,
      child: Column(
        children: <Widget>[
          YAppBar(title: title, canBack: false),
          Container(
            width: yScreenSize().width,
            padding: const EdgeInsets.all(5),
            child: Wrap(
              spacing: 5,
              runSpacing: 5,
              direction: Axis.vertical,
              children: [
                view_button('yToJson', () async {
                  yToast(yToJson({'name': 'aaa'}));
                }),
                view_button('yIsNullOrEmpty', () async {
                  yToast(yIsNullOrEmpty('').toString());
                }),
                view_button('yIsNotEmpty', () async {
                  yToast(yIsNotEmpty('').toString());
                }),
                view_button('yCloseInputMethod', () async {
                  yCloseInputMethod();
                }),
                view_button('yScreenSize', () async {
                  yToast(yScreenSize().toString());
                }),
                view_button('yStatubarHeight', () async {
                  yScreenSize();
                  yToast(yStatubarHeight().toString());
                }),
                view_button('ySafeAreaBottomHeight', () async {
                  yToast(ySafeAreaBottomHeight().toString());
                }),
                view_button('yNavBarHeight', () async {
                  yToast(yNavBarHeight().toString());
                }),
              ],
            ),
          ),
        ],
      ),
    );
  }
}
